package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.core.entity.BaseDO;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ProductBannerAddDTO;
import org.jsola.hr.dto.ProductBannerUpdateDTO;
import org.jsola.hr.entity.ProductBannerDO;
import org.jsola.hr.entity.ProductDO;
import org.jsola.hr.query.ProductBannerQuery;
import org.jsola.hr.service.IProductBannerService;
import org.jsola.hr.service.IProductService;
import org.jsola.hr.vo.ProductBannerListVO;
import org.jsola.hr.vo.ProductBannerVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.validation.Valid;


/**
 * 产品banner
 *
 * @author zhr
 */
@RestController("hrProductBannerController")
@RequestMapping(HrConstants.API_PATH + "/v1/productBanners")
public class ProductBannerController implements IProductBannerControllerAPI {

    @Autowired
    private IProductBannerService productBannerService;

    @Autowired
    private IProductService productService;

    @Override
    @PostMapping
    public Result<ProductBannerVO> save(@Valid @RequestBody ProductBannerAddDTO productBannerAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增产品banner权限校验

        return Result.success(
                productBannerService.save(productBannerAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ProductBannerUpdateDTO productBannerUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改产品banner权限校验

        int count = productBannerService.update(productBannerUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{productBannerId}")
    public Result<Integer> delete(@PathVariable Long productBannerId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除产品banner权限校验

        int count = productBannerService.deleteByIds(tokenUser, productBannerId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] productBannerIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除产品banner权限校验

        int count = productBannerService.deleteByIds(tokenUser, productBannerIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{productBannerId}")
    public Result<ProductBannerVO> findById(@PathVariable Long productBannerId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品banner权限校验

        return Result.success(
            productBannerService.selectById(productBannerId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ProductBannerListVO>> listAll(@Valid ProductBannerQuery productBannerQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品banner权限校验

        return Result.success(
            productBannerService.select(productBannerQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ProductBannerListVO>> page(@Valid ProductBannerQuery productBannerQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            productBannerService.selectPage(productBannerQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectBySite")
    public Result<List<ProductBannerListVO>> selectBySite() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        List<ProductBannerDO> productBannerDOList = productBannerService.selectBySite(tokenUser.getSiteId());
        // 产品信息
        List<ProductDO> productDOList = productService.selectDoByIds(productBannerDOList.parallelStream()
                .map(ProductBannerDO::getProductId).collect(Collectors.toList()), tokenUser.getSiteId());
        // 产品Id——产品链接
        Map<String, String> productIdUrlMap = productDOList.parallelStream()
                .collect(Collectors.toMap(BaseDO::getId, ProductDO::getProductUrl));
        return Result.success(
                productBannerDOList.parallelStream().map(productBannerDO -> {
                    ProductBannerListVO productBannerListVO = productBannerDO.to(ProductBannerListVO.class);
                    // 产品轮播图加入产品详情链接
                    String productUrl = productIdUrlMap.get(productBannerDO.getProductId());
                    productBannerListVO.setProductUrl(productUrl);
                    return productBannerListVO;
                }).parallel().collect(Collectors.toList())
        );
    }


}

